﻿using System;
using System.Web;
using System.Windows.Xps.Packaging;
using System.IO;
using System.Windows.Documents;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Net;
using System.IO.Packaging;
using Microsoft.SharePoint;
using System.Web.UI;
using System.Threading;
using SPThumbnailExtender.Interne;
using System.Drawing;

namespace SPThumbnailExtender
{
    namespace Handler
    {

        public class MobileXPSHandler : IHttpHandler
        {
            /// <summary>
            /// You will need to configure this handler in the web.config file of your 
            /// web and register it with IIS before being able to use it. For more information
            /// see the following link: http://go.microsoft.com/?linkid=8101007
            /// </summary>
            public bool IsReusable
            {
                get { return true; }
            }

            public void ProcessRequest(HttpContext context)
            {
                //context = HttpContext.Current;
                context.Response.ContentType = "image/png";

                string strQXps = context.Request.QueryString["xps"];
                string strWidth = context.Request.QueryString["width"];
                string strHeight = context.Request.QueryString["height"];
                string strCount = context.Request.QueryString["count"];
                string strPage = context.Request.QueryString["n"];
                int? _imgWidth = null;
                int? _imgHeight = null;
                int? _iPage = null;
                bool? _bCount = null;
                bool? _bCache = null;

                int imgWidth;
                if (int.TryParse(strWidth, out imgWidth))
                {
                    _imgWidth = imgWidth;
                }

                int imgHeight;
                if (int.TryParse(strHeight, out imgHeight))
                {
                    _imgHeight = imgHeight;
                }

                int iPage;
                if (int.TryParse(strPage, out iPage))
                {
                    _iPage = iPage;
                }
                bool bCache;
                if (bool.TryParse(context.Request["usecache"], out bCache))
                {
                    _bCache = bCache;
                }


                bool bCount;
                if (bool.TryParse(strCount, out bCount))
                {
                    _bCount = bCount;
                }
                HandlerHelper.ProcessRequest(context, strQXps, _imgWidth, _imgHeight, _iPage, _bCount, _bCache, typeof(ThumbnailXPS));
            }
        }
    }
}